package edu.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;

/**
 * 获取参数的值
 * String getParameter( String name ) ：获取请求中name 对应的参数的值
 * String[] getParameterValues( String name )：获取请求中name对应的参数的值
 * Enumeration getParameterNames()：获得请求中所有的请求参数的名称
 * Map<String , String[]> getParameterMap()：获得请求中所有的参数名称-参数值对应的集合
 */

@WebServlet("/getParameter")
public class GetParameter extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Enumeration<String> parameterNames = request.getParameterNames();
        System.out.println("============使用getParameter获取============");
        while (parameterNames.hasMoreElements()){
            String s = parameterNames.nextElement();
            System.out.println(s+"==>"+request.getParameter(s));
        }
        /**
         * 结果1
         * password==>asdfasdf
         * hobbis==>eat
         * sex==>男
         * username==>张三
         */

        System.out.println("=========使用getParameterValues获取=========");
        Enumeration<String> parameterNames02 = request.getParameterNames();
        while (parameterNames02.hasMoreElements()){
            String s = parameterNames02.nextElement();
            String[] parameterValues = request.getParameterValues(s);
            System.out.println(s+"==>"+ Arrays.toString(parameterValues));
        }
        /**
         * 结果2
         * password==>[asdfasdf]
         * hobbis==>[eat, run]
         * sex==>[男]
         * username==>[张三]
         */

        System.out.println("===========使用getParameterMap获取===========");
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String,String[]>> parameterSet = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : parameterSet) {
            System.out.println(entry.getKey()+"==>"+Arrays.toString(entry.getValue()));
        }
        /**
         * 结果3
         * password==>[asdfasdf]
         * hobbis==>[eat, run]
         * sex==>[男]
         * username==>[张三]
         */

    }
}
